<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function d($data) {
	echo '<pre>';print_r($data);echo '</pre>';
}

function de($data) {
	echo '<pre>';print_r($data);echo '</pre>';
	exit();
}

//
function func_generate_get_tables() {
	global $adb;
	
	$tables = $adb->getTables();
	
	return $tables;
}

function func_generate_get_fields($table) {
	global $adb;
	
	$fields = $adb->getMetadata($table);
	
	return $fields;
}

function func_generate_file_header() {
	$content = "";
	
	$content .= "<?php" . "\n";
	$content .= "/***************************************************************" . "\n";
	$content .= "*  Copyright notice" . "\n";
	$content .= "*  (c) 2009, jianyuzhu@gmail.com" . "\n";
	$content .= "*  All rights reserved" . "\n";
	$content .= "*  This script is part of the PPEMI project." . "\n";
	$content .= "***************************************************************/" . "\n";
	$content .= "\n" . "\n";
	
	return $content;
}

function func_generate_file_footer() {
	$content = "";
	$content .= "//" . "\n";
	$content .= "?>";
	
	return $content;
}

function func_generate_filter_table($table) {
	if( defined('CONFIG_DB_PREFIX') ) {
		$table = substr($table, strlen(CONFIG_DB_PREFIX));
	}
	
	return $table;
}

function func_generate_filter_fields($table, $fields) {
	if( $table && is_array($fields) ) {
		foreach($fields as $k => $field) {
			if( strpos($field['name'], $table) !== false ) {
				$fields[$k]['name'] = substr($field['name'], strlen($table . "_"));
			}
		}
	}
	
	return $fields;
}

function func_generate_filter(&$table, &$fields) {
	if( defined('CONFIG_DB_PREFIX') ) {
		$table = substr($table, strlen(CONFIG_DB_PREFIX));
	}
	if( $table && is_array($fields) ) {
		foreach($fields as $k => $field) {
			if( strpos($field['name'], $table) !== false ) {
				$fields[$k]['name'] = substr($field['name'], strlen($table . "_"));
			}
		}
	}
}

function func_generate_write_file($file, $content) {
	$fp = fopen($file, 'w+');
	fwrite($fp, $content);
	fclose($fp);
}

function func_generate_read_file($file) {
	$content = file_get_contents($file);
	
	return $content;
}

function func_generate_model($table_param, $fields_param) {
	if( !$table_param || !is_array($fields_param) ) {
		return false;
	}
	$table = $table_param;
	$fields = $fields_param;
	//func_generate_filter($table, $fields);
	if( defined('CONFIG_DB_PREFIX') && substr($table, 0, strlen(CONFIG_DB_PREFIX)) == CONFIG_DB_PREFIX ) {
		$table = substr($table, strlen(CONFIG_DB_PREFIX));
	}
	$field_table = $table;
	if( strpos($table, '_') ) {
		$field_prefix = substr($table, 0, strpos($table, '_')) . '_';
	} else {
		$field_prefix = $table . '_';
	}
	$classname = "";
	$table_tmp = explode("_", $table);
	if( sizeof($table_tmp) > 0 ) {
		$functionname = "";
		foreach($table_tmp as $tmpk => $tmpv) {
			$classname .= ucfirst($tmpv);
		}
	} else {
		$classname = ucfirst($table);
	}
	
	$fields_name = array();
	$fields_type = array();
	$fields_sname = array();
	$fields_key = array();
	reset($fields);
	foreach($fields as $k => $field) {
		$fields_name[] = $field['name'];
		$fields_type[] = $field['type'];
		if( strpos($field['flags'], 'primary_key') ) {
			if( substr($field['name'], 0, strlen($field_prefix)) == $field_prefix ) {
				$fields_key[] = substr($field['name'], strlen($field_prefix));
			} else {
				$fields_key[] = $field['name'];
			}
		}
		if( substr($field['name'], 0, strlen($field_prefix)) == $field_prefix ) {
			$fields[$k]['shortname'] = substr($field['name'], strlen($field_prefix));
		} else {
			$fields[$k]['shortname'] = $field['name'];
		}
		$fields_sname[] = $fields[$k]['shortname'];
	}
	
	$content = "";
	$content .= func_generate_file_header();
	//
	$content .= "class " . $classname . " {" . "\n";
	$content .= "	var $" . "field_table" . " = '" . $field_table . "';" . "\n";
	//$content .= "	var $" . "field_prefix" . " = '" . $field_prefix . "';" . "\n";
	$content .= "	var $" . "field_key" . " = array('" . implode("', '", $fields_key) . "');" . "\n";
	$content .= "	var $" . "fields_name" . " = array('" . implode("', '", $fields_name) . "');" . "\n";
	$content .= "	var $" . "fields_type" . " = array('" . implode("', '", $fields_type) . "');" . "\n";
	$content .= "	var $" . "fields_sname" . " = array('" . implode("', '", $fields_sname) . "');" . "\n";
	$content .= "\n";
	reset($fields);
	foreach($fields as $k => $field) {
		$content .= "	var $" . $field['shortname'] . ";" . "\n";
	}
	//
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//constructor" . "\n";
	$content .= "	function " . $classname . "() {" . "\n";
	$content .= "		//init here" . "\n";
	$content .= "		" . "\n";
	$content .= "	}" . "\n";
	//key
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//key" . "\n";
	if( sizeof($fields_key) > 1 ) {
		$content .= "	function " . $classname . "() {" . "\n";
		$content .= "		return $";
		foreach($fields_key as $k => $key) {
			if( $k == 0 ) {
				$content .= "this->" . $key;
			} else {		
				$content .= " . '_' . " . "$" . "this->" . $key;
			}
		}
		$content .= ";" . "\n";
		$content .= "	}" . "\n";
	} else {
		$content .= "	function getKey() {" . "\n";
		$content .= "		return $" . "this->" . $fields_key['0'] . ";\n";
		$content .= "	}" . "\n";
	}
	
	//setter/getter
	reset($fields);
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//setter/getter" . "\n";
	foreach($fields as $k => $field) {
		$fieldname = $field['shortname'];
		$fieldname_tmp = explode("_", $fieldname);
		if( sizeof($fieldname_tmp) > 0 ) {
			$functionname = "";
			foreach($fieldname_tmp as $tmpk => $tmpv) {
				$functionname .= ucfirst($tmpv);
			}
		} else {
			$functionname = ucfirst($field['name']);
		}
		
		$content .= "	function set" . $functionname . "($" . $fieldname . ") {" . "\n";
		$content .= "		$" . "this->" . $fieldname . " = $" . $fieldname . ";" . "\n";
		$content .= "	}" . "\n";
		$content .= "	\n";
		$content .= "	function get" . $functionname . "() {" . "\n";
		$content .= "		return $" . "this->" . $fieldname . ";" . "\n";
		$content .= "	}" . "\n";
		$content .= "	\n";
	}
	//
	$content .= "}" . "\n";
	$content .= func_generate_file_footer();
	
	//
	$filename = strtolower($classname) . '.model.class.php';
	$file = RT_GENERATE_MODEL_PATH . $filename;
	func_generate_write_file($file, $content);
	
	return $classname;
}

function func_generate_dao($table_param, $fields_param) {
	if( !$table_param || !is_array($fields_param) ) {
		return false;
	}
	$table = $table_param;
	$fields = $fields_param;
	//func_generate_filter($table, $fields);
	if( defined('CONFIG_DB_PREFIX') && substr($table, 0, strlen(CONFIG_DB_PREFIX)) == CONFIG_DB_PREFIX ) {
		$table = substr($table, strlen(CONFIG_DB_PREFIX));
	}
	$field_table = $table;
	if( strpos($table, '_') ) {
		$field_prefix = substr($table, 0, strpos($table, '_')) . '_';
	} else {
		$field_prefix = $table . '_';
	}
	$classname = "";
	$table_tmp = explode("_", $table);
	if( sizeof($table_tmp) > 0 ) {
		$functionname = "";
		foreach($table_tmp as $tmpk => $tmpv) {
			$classname .= ucfirst($tmpv);
		}
	} else {
		$classname = ucfirst($table);
	}
	
	$fields_name = array();
	$fields_type = array();
	$fields_sname = array();
	$fields_key = array();
	reset($fields);
	foreach($fields as $k => $field) {
		$fields_name[] = $field['name'];
		$fields_type[] = $field['type'];
		if( strpos($field['flags'], 'primary_key') ) {
			if( substr($field['name'], 0, strlen($field_prefix)) == $field_prefix ) {
				$fields_key[] = substr($field['name'], strlen($field_prefix));
			} else {
				$fields_key[] = $field['name'];
			}
		}
		if( substr($field['name'], 0, strlen($field_prefix)) == $field_prefix ) {
			$fields[$k]['shortname'] = substr($field['name'], strlen($field_prefix));
		} else {
			$fields[$k]['shortname'] = $field['name'];
		}
		$fields_sname[] = $fields[$k]['shortname'];
	}
}

function func_generate_base($table_param, $fields_param) {
	if( !$table_param || !is_array($fields_param) ) {
		return false;
	}
	$table = $table_param;
	$fields = $fields_param;
	//func_generate_filter($table, $fields);
	if( defined('CONFIG_DB_PREFIX') && substr($table, 0, strlen(CONFIG_DB_PREFIX)) == CONFIG_DB_PREFIX ) {
		$table = substr($table, strlen(CONFIG_DB_PREFIX));
	}
	$field_table = $table;
	if( strpos($table, '_') ) {
		$field_prefix = substr($table, 0, strpos($table, '_')) . '_';
	} else {
		$field_prefix = $table . '_';
	}
	$classname = "";
	$table_tmp = explode("_", $table);
	if( sizeof($table_tmp) > 0 ) {
		$functionname = "";
		foreach($table_tmp as $tmpk => $tmpv) {
			$classname .= ucfirst($tmpv);
		}
	} else {
		$classname = ucfirst($table);
	}
	
	$fields_name = array();
	$fields_type = array();
	$fields_sname = array();
	$fields_key = array();
	reset($fields);
	foreach($fields as $k => $field) {
		$fields_name[] = $field['name'];
		$fields_type[] = $field['type'];
		if( strpos($field['flags'], 'primary_key') ) {
			if( substr($field['name'], 0, strlen($field_prefix)) == $field_prefix ) {
				$fields_key[] = substr($field['name'], strlen($field_prefix));
			} else {
				$fields_key[] = $field['name'];
			}
		}
		if( substr($field['name'], 0, strlen($field_prefix)) == $field_prefix ) {
			$fields[$k]['shortname'] = substr($field['name'], strlen($field_prefix));
		} else {
			$fields[$k]['shortname'] = $field['name'];
		}
		$fields_sname[] = $fields[$k]['shortname'];
	}
	
	$content = "";
	$content .= func_generate_file_header();
	//
	$content .= "class " . $classname . "Base extends Base {" . "\n";
	$content .= "	var $" . "field_table" . " = '" . $field_table . "';" . "\n";
	$content .= "	var $" . "field_key" . " = array('" . implode("', '", $fields_key) . "');" . "\n";
	$content .= "	var $" . "fields_name" . " = array('" . implode("', '", $fields_name) . "');" . "\n";
	$content .= "	var $" . "fields_type" . " = array('" . implode("', '", $fields_type) . "');" . "\n";
	$content .= "	var $" . "fields_sname" . " = array('" . implode("', '", $fields_sname) . "');" . "\n";
	//
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//constructor" . "\n";
	$content .= "	function " . $classname . "Base() {" . "\n";
	//$content .= "		global $" . "adb;" . "\n";
	//$content .= "		$" . "this->adb = $" . "adb;" . "\n";
	$content .= "		//init here" . "\n";
	$content .= "		" . "\n";
	$content .= "	}" . "\n";
	//key
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//key" . "\n";
	if( sizeof($fields_key) > 1 ) {
		$content .= "	function " . $classname . "() {" . "\n";
		$content .= "		return $";
		foreach($fields_key as $k => $key) {
			if( $k == 0 ) {
				$content .= "this->" . $key;
			} else {		
				$content .= " . '_' . " . "$" . "this->" . $key;
			}
		}
		$content .= ";" . "\n";
		$content .= "	}" . "\n";
	} else {
		$content .= "	function getKey() {" . "\n";
		$content .= "		return $" . "this->" . $fields_key['0'] . ";\n";
		$content .= "	}" . "\n";
	}
	
	//methods
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//methods" . "\n";
	//ADD
	$content .= "	//add" . "\n";
	$content .= '	function add($value, $type = \'obj\') {' . "\n";
	$content .= '		if( $type == \'obj\' ) {' . "\n";
	$content .= '			$this->addFromObject($value);' . "\n";
	$content .= '		} elseif( $type == \'array\' ) {' . "\n";
	$content .= '			$this->addFromArray($value);' . "\n";
	$content .= '		}' . "\n";
	$content .= '	}' . "\n";
	$content .= "	\n";
	$content .= '	//param is array' . "\n";
	$content .= '	function addFromArray($obj) {' . "\n";
	//$content .= '		//$data_array = func_db_input($data_array);' . "\n";
	$content .= '		if( !is_object($obj) && get_class($obj) != \'' . $classname . '\' ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_array($obj, \'insert\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		if( $result )' . "\n";
	$content .= '			$id = $this->adb->insert_id();' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $id;' . "\n";
	$content .= "	}" . "\n";
	$content .= "	\n";
	$content .= '	//param is ' . $classname . ' object' . "\n";
	$content .= '	function addFromObject($obj) {' . "\n";
	//$content .= '		//$obj = func_db_input($obj);' . "\n";
	$content .= '		if( !is_object($obj) && get_class($obj) != \'' . $classname . '\' ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_obj($obj, \'insert\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		if( $result )' . "\n";
	$content .= '			$id = $this->adb->insert_id();' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $id;' . "\n";
	$content .= "	}" . "\n";
	
	//SELECT
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//select" . "\n";
	$content .= '	function get($value, $type = \'obj\') {' . "\n";
	$content .= '		if( $type == \'obj\' ) {' . "\n";
	$content .= '			$this->getToObject($value);' . "\n";
	$content .= '		} elseif( $type == \'array\' ) {' . "\n";
	$content .= '			$this->getToArray($value);' . "\n";
	$content .= '		} else {' . "\n";
	$content .= '			$this->getByKey($value);' . "\n";
	$content .= '		}' . "\n";
	$content .= '	}' . "\n";
	$content .= "	\n";
	$content .= '	//param is key' . "\n";
	$content .= '	function getByKey($key) {' . "\n";
	$content .= '		if( strlen($key) == 0 ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_key($key, \'select\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		$row = $this->adb->fetch_array($result);' . "\n";
	$content .= '		$obj = $this->_array_to_obj($row, $this->classname);' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $obj;' . "\n";
	$content .= "	}" . "\n";
	$content .= "	\n";
	$content .= '	//param is array' . "\n";
	$content .= '	function getToArray($data_array) {' . "\n";
	$content .= '		if( !is_array($data_array) ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_array($data_array, \'select\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		if( $result )' . "\n";
	$content .= '			$id = $this->adb->insert_id();' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $id;' . "\n";
	$content .= "	}" . "\n";
	$content .= "	\n";
	$content .= '	//param is ' . $classname . ' object' . "\n";
	$content .= '	function getToObject($obj) {' . "\n";
	$content .= '		if( !is_object($obj) && get_class($obj) != \'' . $classname . '\' ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_obj($obj, \'select\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		if( $result )' . "\n";
	$content .= '			$id = $this->adb->insert_id();' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $id;' . "\n";
	$content .= "	}" . "\n";
	$content .= "	\n";
	$content .= '	//get list' . "\n";
	$content .= '	//filter' . "\n";
	$content .= '	function getList($filter) {' . "\n";
	$content .= '		$list = array();' . "\n";
	$content .= '		$sql = $this->_sql_list($filter, \'select\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		while($row = $this->adb->fetch_array($result))' . "\n";
	$content .= '			$list[] = $row;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $list;' . "\n";
	$content .= "	}" . "\n";
	$content .= "	\n";
	$content .= '	//get total' . "\n";
	$content .= '	//filter' . "\n";
	$content .= '	function getCount($filter) {' . "\n";
	$content .= '		$sql = $this->_sql_count($filter);' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		$row = $this->adb->fetch_array($result);' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $row[\'count\'];' . "\n";
	$content .= "	}" . "\n";
	$content .= "	\n";
	
	//UPDATE
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//update" . "\n";
	$content .= '	function update($value, $type = \'obj\') {' . "\n";
	$content .= '		if( $type == \'obj\' ) {' . "\n";
	$content .= '			$this->updateFromObject($value);' . "\n";
	$content .= '		} elseif( $type == \'array\' ) {' . "\n";
	$content .= '			$this->updateFromArray($value);' . "\n";
	$content .= '		}' . "\n";
	$content .= '	}' . "\n";
	$content .= "	\n";
	$content .= '	//param is array' . "\n";
	$content .= '	function updateFromArray($data_array) {' . "\n";
	$content .= '		if( !is_array($data_array) ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_array($data_array, \'update\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		if( $result )' . "\n";
	$content .= '			$id = $this->adb->insert_id();' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $id;' . "\n";
	$content .= "	}" . "\n";
	$content .= "	\n";
	$content .= '	//param is ' . $classname . ' object' . "\n";
	$content .= '	function updateFromObject($obj) {' . "\n";
	$content .= '		if( !is_object($obj) && get_class($obj) != \'' . $classname . '\' ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_obj($obj, \'update\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		if( $result )' . "\n";
	$content .= '			$id = $this->adb->insert_id();' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $id;' . "\n";
	$content .= "	}" . "\n";
	
	//DELETE
	$content .= "	\n";
	$content .= "	\n";
	$content .= "	//delete" . "\n";
	$content .= '	function delete($value, $type = \'obj\') {' . "\n";
	$content .= '		if( $type == \'obj\' ) {' . "\n";
	$content .= '			$this->deleteFromObject($value);' . "\n";
	$content .= '		} elseif( $type == \'array\' ) {' . "\n";
	$content .= '			$this->deleteFromArray($value);' . "\n";
	$content .= '		}' . "\n";
	$content .= '	}' . "\n";
	$content .= "	\n";
	$content .= '	//param is array' . "\n";
	$content .= '	function deleteFromArray($data_array) {' . "\n";
	$content .= '		if( !is_array($data_array) ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_array($data_array, \'delete\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		if( $result )' . "\n";
	$content .= '			$id = $this->adb->insert_id();' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $id;' . "\n";
	$content .= "	}" . "\n";
	$content .= "	\n";
	$content .= '	//param is ' . $classname . ' object' . "\n";
	$content .= '	function deleteFromObject($obj) {' . "\n";
	$content .= '		if( !is_object($obj) && get_class($obj) != \'' . $classname . '\' ) {' . "\n";
	$content .= '			return false;' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		$id = -1;' . "\n";
	$content .= '		$sql = $this->_sql_obj($obj, \'delete\');' . "\n";
	$content .= '		$result = $this->adb->query($sql);' . "\n";
	$content .= '		if( $result )' . "\n";
	$content .= '			$id = $this->adb->insert_id();' . "\n";
	$content .= '		}' . "\n";
	$content .= '		' . "\n";
	$content .= '		return $id;' . "\n";
	$content .= "	}" . "\n";
	
	//
	$content .= "}" . "\n";
	$content .= func_generate_file_footer();
	
	//
	$filename = strtolower($classname) . '.base.class.php';
	$file = RT_GENERATE_BASE_PATH . $filename;
	func_generate_write_file($file, $content);
	
	return $classname;
}
//
?>